import type {App} from 'vue'
import nCarousel from '@/components/carousel/index.vue'
import nCarouselItem from '@/components/carousel/item/index.vue'
import { useGlobalStore } from '@/store/module/global'

//获得components文件夹下的所有vue文件
const requierComponent = [
    import.meta.glob('./modal/components/add/componentList/*.vue')
]

export const registerGlobalComponent = (app: App) => {
    app.component('nCarousel', nCarousel)
    app.component('nCarouselItem', nCarouselItem)
    registerFlies(app)
}

const componentList: Record<string, Component> = {}
const registerFlies = (app: App) => {
    const globalStore = useGlobalStore()
    requierComponent.forEach(item => {
        const files = Object.keys(item)
        files.forEach(async file => {
            const component = await item[file]() as any
            let componentName = file.replace(/^.+\/([^/]+)\.vue$/, '$1')
            componentList[componentName] = shallowRef(component.default)
            globalStore.componentList = componentList
            app.component(componentName, component)
        })
    })
}